# file:: $Name$
# author::  $Author$
# version:: $Revision$
# date:: $Date$
#
# This source code copyright (C) 2006 by Ralph M. Churchill
# All rights reserved.
#
# Released under the terms of the GNU General Public License
# See LICENSE file for additional information.
require 'engine/command'

class SpawnCharacter < Command
    def initialize(executor)
        super(executor,'spawnchar','spawnchar <character template> | [list]',
              'Spawn a character in the current room.')
    end
    def execute(args)
        raise UsageException if args.empty?
        template = args.join
        case template
        when /\d/
            template_id = template.to_i
        when /\w/
            if template =~ /^list/i
                inform(CharacterDatabase.instance.templates.map do |c|
                    "#{c.oid} - #{c.name}"
                end.join(', '))
                return
            else
                if t = CharacterDatabase.instance.find_template_by_name(template)
                    template_id = t.oid
                else
                    inform("Template: #{template} not found.")
                    return
                end
            end
        end
        GameEngine.instance.do_action(Action.new(:spawncharacter,self.executor,
                                                 {:character => template_id,
                                                 :where => self.executor.room}))
    end
end

class SpawnItem < Command
    def initialize(executor)
        super(executor,'spawnitem','spawnitem <item template> | [list]',
              'Spawn a item in your inventory.')
    end
    def execute(args)
        raise UsageException if args.empty?
        template = args.join
        case template
        when /\d/
            template_id = template.to_i
        when /\w/
            if template =~ /^list/i
                inform(ItemDatabase.instance.templates.map do |c|
                    "#{c.oid} - #{c.name}"
                end.join(', '))
                return
            else
                if t = ItemDatabase.instance.find_template_by_name(template)
                    template_id = t.oid
                else
                    inform("Template: #{template} not found.")
                    return
                end
            end
        end
        GameEngine.instance.do_action(Action.new(:spawnitem,self.executor,
                                                 {:item => template_id,
                                                 :where => self.executor}))
    end
end

class Reload < Command
    def initialize(executor)
        super(executor,'reload','reload <commands|logic|>',
             'Reload Commands, Logic, ...')
    end
    def execute(args)
        raise UsageException if args.empty?
        case args.join
        when /command/
            GameEngine.instance.load_command_directories
        when /logic/
            GameEngine.instance.load_logic_directories
        else
            inform("#{args.join} cannot be reloaded.")
        end
    end
end
